package com.caigou.service.impl;

import com.caigou.bean.cms.entity.*;
import com.caigou.bean.cms.param.*;
import com.caigou.bean.param.FrontChannelDataPram;
import com.caigou.bean.vo.*;
import com.caigou.component.*;
import com.caigou.enums.Constant;
import com.caigou.enums.PrivilegeEnum;
import com.caigou.enums.ResponseEnum;
import com.caigou.mapper.cms.*;
import com.caigou.pojo.*;
import com.caigou.service.ArticleDictionaryService;
import com.caigou.mapper.cms.ChannelConfigMapper;
import com.caigou.service.ChannelService;
import com.caigou.service.CollectionService;
import com.caigou.util.PinyinUtil;
import com.caigou.util.Result;
import com.caigou.util.ResultUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ChannelServiceImpl implements ChannelService {

    @Resource
    private CollectionService collectionService;
    @Resource
    private ArticleDictionaryService articleDictionaryService;

    @Resource
    private ChannelMapper channelMapper;
    @Resource
    private ChannelConfigMapper channelConfigMapper;
    @Resource
    private ChannelDataMapper channelDataMapper;
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private ArticleTitleMapper articleTitleMapper;
    @Resource
    private ChannelDataInfoMapper channelDataInfoMapper;
    @Resource
    private ArticleDictionaryMapper articleDictionaryMapper;
    @Resource
    private UserPrivilegeMapper userPrivilegeMapper;
    @Resource
    private UserBaseInfoMapper userBaseInfoMapper;
    @Resource
    private ArticleHtmlMapper articleHtmlMapper;
    @Resource
    private HtmlBuilderComponent htmlBuilderComponent;
    @Resource
    private HtmlHomePageBuilderComponent htmlHomePageBuilderComponent;
    @Resource
    private HtmlDetailBuilderComponent htmlDetailBuilderComponent;
    @Resource
    private HtmlListPageBuilderComponent htmlListPageBuilderComponent;
    @Resource
    private AmqpTemplate amqpTemplate;
    @Resource
    private RedisObjectComponent redisObjectComponent;
    @Resource
    private ArticleStatisticsDayMapper articleStatisticsDayMapper;

    @Override
    public Result loadChannelConfigList(ChannelParam param) {
        List<ChannelConfigList> list = channelMapper.loadChannelConfigList(param.getChannelId());
        if (null != list) {
            // 遍历处理频道展示图
            list.forEach(item -> {
                item.setChannelImage(channelMapper.loadChannelConfigInfo(item.getChannelId()));
            });
        }
        return Result.success(list);
    }

    @Override
    public Result updateChannelConfig(ChannelParam param) {
        // 修改频道配置数据
        channelMapper.updateChannelConfig(param);
        // 获取当前频道已经推荐的数量
        List<ChannelDataList> list = channelDataMapper.loadChannelRecommendDataList(param.getChannelId());
        int recommendCount = param.getChannelCount();
        if (null != list) {
            // 特殊频道个数需要单独计算
            switch (param.getChannelId()) {
                case "101010301":// 首页-图表数据(2倍)
                case "104010201":// 监管-新规解读(2倍)
                case "105010201":// 研究院-政采大数据(2倍)
                case "107010201":// 汽车-图表数据
                case "108010201":// IT-图表数据
                case "109010201":// 电梯-图表数据
                case "110010201":// 空调-图表数据
                case "111010201":// 家具-图表数据
                    recommendCount = recommendCount * 2;
                    break;
                case "101010201":// 首页-易采通精品课(3倍)
                case "101010501":// 首页-正福易考通(3倍)
                case "102010201":// 采购人-高校院所(3倍)
                case "103010201":// 实务-亚利说采购(3倍)
                case "107010302":// 汽车-图例展示
                case "108010302":// IT-图例展示
                case "109010302":// 电梯-图例展示
                case "110010302":// 空调-图例展示
                case "111010302":// 家具-图例展示
                    recommendCount = recommendCount * 3;
                    break;
            }
            // 当前频道已经推荐了内容--判定推荐的内容数量与配置的数量
            if (list.size() > recommendCount) {
                // 频道修改后的数量比原推荐数量小--需要进行去除推荐操作
                for (int i = recommendCount; i < list.size(); i++) {
                    channelDataMapper.updateChannelDataRecommendStatus(list.get(i).getId(), 0);
                }
            }
        }
        // 设置更新接口 如果当前是这几组数据则更新频道头与频道尾
        switch (param.getChannelId()) {
            case "100010601":// 全局--页眉手机移动端指向二维码--11个频道头
                htmlBuilderComponent.buildHeaderHtml("101");// 更新首页频道头
                htmlBuilderComponent.buildHeaderHtml("102");// 更新采购人频道头
                htmlBuilderComponent.buildHeaderHtml("103");// 更新采购实务频道头
                htmlBuilderComponent.buildHeaderHtml("104");// 更新监管频道头
                htmlBuilderComponent.buildHeaderHtml("105");// 更新研究院频道头
                htmlBuilderComponent.buildHeaderHtml("106");// 更新军队频道头
                htmlBuilderComponent.buildHeaderHtml("107");// 更新汽车频道头
                htmlBuilderComponent.buildHeaderHtml("108");// 更新IT频道头
                htmlBuilderComponent.buildHeaderHtml("109");// 更新电梯频道头
                htmlBuilderComponent.buildHeaderHtml("110");// 更新空调电器频道头
                htmlBuilderComponent.buildHeaderHtml("111");// 更新家具频道头
                break;
            case "100011001":// 全局--尾部易采通下载二维码--频道尾
                htmlBuilderComponent.buildFooterHtml();
                break;
        }
        if (param.getChannelId().length() >= 9) {
            // 更新的是栏目层级--数据更新完成之后更新碎片显示及文字状态
            htmlHomePageBuilderComponent.buildHomePageHtml(param.getChannelId());
        }
        // 同步更新处理
        if (param.getChannelId().equals("104010301")){
            // 当更新监管法律法规的时候--同步更新分站国家法规数据信息
            ChannelParam param1;
            param1 = param;
            param1.setChannelId("201010104");
            channelMapper.updateChannelConfig(param);// 河北--国家法规
            param1.setChannelId("301010104");
            channelMapper.updateChannelConfig(param);// 河南--国家法规
            param1.setChannelId("401010104");
            channelMapper.updateChannelConfig(param);// 山东--国家法规
            param1.setChannelId("601010104");
            channelMapper.updateChannelConfig(param);// 辽宁--国家法规
            param1.setChannelId("901010104");
            channelMapper.updateChannelConfig(param);// 江苏--国家法规
            param1.setChannelId("1001010104");
            channelMapper.updateChannelConfig(param);// 浙江--国家法规
            param1.setChannelId("1201010104");
            channelMapper.updateChannelConfig(param);// 福建--国家法规
            param1.setChannelId("1401010104");
            channelMapper.updateChannelConfig(param);// 湖北--国家法规
            param1.setChannelId("1701010104");
            channelMapper.updateChannelConfig(param);// 海南--国家法规
            param1.setChannelId("1901010104");
            channelMapper.updateChannelConfig(param);// 贵州--国家法规
            param1.setChannelId("2001010104");
            channelMapper.updateChannelConfig(param);// 云南--国家法规
            param1.setChannelId("2101010104");
            channelMapper.updateChannelConfig(param);// 陕西--国家法规
            param1.setChannelId("2401010104");
            channelMapper.updateChannelConfig(param);// 北京--国家法规
            param1.setChannelId("2501010104");
            channelMapper.updateChannelConfig(param);// 天津--国家法规
            param1.setChannelId("2601010104");
            channelMapper.updateChannelConfig(param);// 重庆--国家法规
            param1.setChannelId("3801010104");
            channelMapper.updateChannelConfig(param);// 深圳--国家法规
            param1.setChannelId("3501010104");
            channelMapper.updateChannelConfig(param);// 青岛--国家法规
            param1.setChannelId("3401010104");
            channelMapper.updateChannelConfig(param);// 大连--国家法规
            param1.setChannelId("3201010104");
            channelMapper.updateChannelConfig(param);// 西藏--国家法规
            param1.setChannelId("3101010104");
            channelMapper.updateChannelConfig(param);// 新疆--国家法规
            param1.setChannelId("3001010104");
            channelMapper.updateChannelConfig(param);// 宁夏--国家法规
            param1.setChannelId("2901010104");
            channelMapper.updateChannelConfig(param);// 广西--国家法规
            param1.setChannelId("2801010104");
            channelMapper.updateChannelConfig(param);// 内蒙古--国家法规
            param1.setChannelId("2201010104");
            channelMapper.updateChannelConfig(param);// 甘肃--国家法规
            param1.setChannelId("3301010104");
            channelMapper.updateChannelConfig(param);// 新疆生产建设兵团--国家法规
            // 更新当前分站静态页
            htmlHomePageBuilderComponent.buildHomePageHtml("201010104");// 河北--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("301010104");// 河南--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("401010104");// 山东--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("601010104");// 辽宁--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("901010104");// 辽宁--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1001010104");// 浙江--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1201010104");// 福建--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1401010104");// 湖北--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1701010104");// 海南--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1901010104");// 贵州--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2001010104");// 云南--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2101010104");// 陕西--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2401010104");// 北京--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2501010104");// 天津--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2601010104");// 重庆--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3801010104");// 深圳--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3501010104");// 青岛--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3401010104");// 大连--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3201010104");// 西藏--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3101010104");// 新疆--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3001010104");// 宁夏--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2901010104");// 广西--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2801010104");// 内蒙古--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2201010104");// 甘肃--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3301010104");// 新疆生产建设兵团--国家法规
        }
        return Result.success(ResponseEnum.SUCCESS);
    }

    @Override
    public Result saveChannelData(List<ChannelDataParam> param) {
        int successCount = 0;
        int errorCount = 0;
        for (ChannelDataParam item : param) {
            // 判断当前内容是否存在于当前频道内
            ChannelData isHave = channelDataMapper.loadChannelIsHaveData(item.getPlateId(), item.getArticleId());
            log.info("loadArticleIsHave articleId={}, plateId={}, isHave={}", item.getArticleId(), item.getPlateId(), isHave);
            if (null == isHave) {
                // 当前频道不存在内容--可以添加
                Article article = articleMapper.getArticle(item.getArticleId());
                if (item.getPlateId().equals("107020201") && article.getArticleType() != 7){
                    // 好车汇无法添加产品以外的类型
                    errorCount += 1;
                } else if (item.getPlateId().equals("100010101") && article.getArticleType() != 8) {
                    // 精品好书无法添加图书类型以外的文章
                    errorCount += 1;
                } else if (item.getPlateId().equals("100010201") && article.getArticleType() != 6) {
                    // 亚利聊政采无法添加亚利聊政采类型以外的文章
                    errorCount += 1;
                } else {
                    // 判断当前文章是否有对应
                    int titleType = channelConfigMapper.loadTitleTypeConfig(item.getPlateId());
                    // 添加对应的板块数据
                    ChannelData channelData = new ChannelData();
                    channelData.setTitleType(titleType);// 如果请求为推荐列表，那么取值为板块对应的标题类型
                    channelData.setArticleId(item.getArticleId());
                    channelData.setPlateId(item.getPlateId());
                    channelData.setDataType(article.getArticleType());
                    channelData.setShowIndex(0);
                    channelData.setRecommendStatus(0);// 推荐状态默认为0
                    channelData.setStickStatus(0);// 置顶状态默认为0
                    channelData.setLockStatus(0);// 锁定状态默认为0
                    int result = channelDataMapper.saveChannelData(channelData);
                    if (result > 0) {
                        successCount += 1;
                        // 如果是第一次发布到频道，则生成文章的频道静态页
                        // 获取当前文章除RSS频道以外发布的最早的记录--如果没有记录则更新RSS频道，如果有则更新最早的数据
                        ChannelData channel = channelDataMapper.loadLastChannelData(item.getArticleId());
                        log.info("loadLastChannelData by articleId={} plateId={} channel={}", item.getArticleId(), item.getPlateId(), channel);
                        if (null == channel) {
                            htmlDetailBuilderComponent.buildArticleHtml(item.getArticleId(), "999");
                        } else {
                            htmlDetailBuilderComponent.buildArticleHtml(item.getArticleId(), channel.getPlateId());
                        }
                        // 文章修改的时候，清除文章缓存(移动端请求改为从数据库获取)
                        redisObjectComponent.del(item.getArticleId());
                    } else {
                        errorCount += 1;
                    }
                }
            } else {
                // 当前频道已存在相同内容--不能添加
                errorCount += 1;
            }
        }
        return ResultUtil.success("发布成功" + successCount + "条，失败" + errorCount + "条");
    }

    @Override
    public Result deleteChannelData(ChannelDataParam param) {
        // 判定当前内容是否在当前频道的推荐中
        int recommendStatus = channelDataMapper.loadChannelDataRecommendStatus(param.getId());
        //推荐状态下的内容无法删除
        if (recommendStatus == 1) return ResultUtil.error(ResponseEnum.CHANNEL_RECOMMEND_LIST_DELETE_ERROR);
        // 进行大列表数据删除(直接删除当前频道推送的ID数据)
        channelDataMapper.deleteChannelData(param.getId());

        // 处理频道发布数据
        List<ChannelData> list = channelDataMapper.loadChannelDataListByArticle(param.getArticleId());
        if (list.size() == 1) {
            // 清除数据之后只剩下RSS频道，则将文章回滚到RSS频道内容
            htmlDetailBuilderComponent.buildArticleHtml(param.getArticleId(), "999");
            redisObjectComponent.del(param.getArticleId());
        } else {
            for (int i = list.size() - 1; i >= 0; i--) {
                if (!list.get(i).getPlateId().equals("-1")) {
                    // 以频道发布时间倒叙遍历--当遍历到非RSS频道的第一条数据的时候，将当前文章设置为当前频道内容
                    htmlDetailBuilderComponent.buildArticleHtml(param.getArticleId(), list.get(i).getPlateId());
                    // 文章修改的时候，清除文章缓存(移动端请求改为从数据库获取)
                    redisObjectComponent.del(param.getArticleId());
                    break;
                }
            }
        }
        return ResultUtil.success(ResponseEnum.SUCCESS);
    }

    @Override
    public Result updateChannelData(ChannelDataParam param) {
        // 修改对应的标题数据
        ArticleTitle articleTitle = new ArticleTitle();
        articleTitle.setArticleId(param.getArticleId());
        articleTitle.setArticleTitleType(-1);
        articleTitle.setArticleTitle(param.getArticleTitle());
        articleTitle.setArticleImageId(param.getArticleImageId());
        articleTitle.setArticleImage(param.getArticleImage());
        articleTitle.setArticleImageAlt(param.getArticleImageAlt());
        articleTitleMapper.updateArticleTitleDataBySelective(articleTitle);

        // 保存图片使用记录
        ChannelDataInfo channelDataInfo = new ChannelDataInfo();
        channelDataInfo.setPlateId(param.getPlateId());
        channelDataInfo.setArticleId(param.getArticleId());
        channelDataInfo.setFileId(articleTitle.getArticleImageId());
        channelDataInfo.setFileType(1);
        channelDataInfoMapper.saveChannelDataInfoData(channelDataInfo);

        // 修改任何一项文章内容，发送ESMQ消息
        amqpTemplate.convertAndSend(Constant.CMS_ARTICLE_QUEUE, Collections.singletonList(new EsArticleMqVO(param.getArticleId(), Constant.UPDATE_ARTICLE)));
        return ResultUtil.success(ResponseEnum.SUCCESS);
    }

    @Override
    public Result loadChannelDataList(ChannelDataListParam param) {
        PageHelper.startPage(param.getPage(), param.getSize());
        PageInfo<ChannelDataList> list = new PageInfo<>(channelDataMapper.loadChannelDataList(param));
        list.getList().forEach(item -> {
            // 获取改文章对应类型标题
            ArticleTitle articleTitle = articleTitleMapper.loadArticleTitle(item.getArticleId(), -1);// 大列表标题类型固定为-1
            // 设置文章的标题类型数据
            item.setArticleTitle(articleTitle.getArticleTitle());
            item.setArticleTitleImage(articleTitle.getArticleImage());
            item.setArticleTitleImageAlt(articleTitle.getArticleImageAlt());
            item.setArticleTitleImageId(articleTitle.getArticleImageId());
        });
        return ResultUtil.success(list);
    }

    @Override
    public Result updateChannelDataStickStatus(ChannelDataParam param) {
        // 列表状态下设置置顶顺序
        if (null == param.getStickStatus() || param.getStickStatus() == 1) {
            // 置顶操作
            int maxStick = channelDataMapper.loadChannelDataMaxStick(param.getPlateId());
            channelDataMapper.updateChannelDataStickStatus(param.getId(), maxStick + 1);
        } else {
            channelDataMapper.updateChannelDataStickStatus(param.getId(), 0);
        }
        return ResultUtil.success(ResponseEnum.SUCCESS);
    }

    @Override
    public Result updateChannelDataRecommendStatus(ChannelDataParam param) {
        // 判定当前频道所配置的最大内容量
        int maxCount = channelMapper.loadChannelConfigCount(param.getPlateId());// 当前频道配置的最大数量
        // 倍数控制处理
        switch (param.getPlateId()) {
            case "101010301":// 首页-图表数据(2倍)
            case "104010201":// 监管-新规解读(2倍)
            case "105010201":// 研究院-政采大数据(2倍)
            case "107010201":// 汽车-图表数据
            case "108010201":// IT-图表数据
            case "109010201":// 电梯-图表数据
            case "110010201":// 空调-图表数据
            case "111010201":// 家具-图表数据
                maxCount = maxCount * 2;
                break;
            case "101010201":// 首页-易采通精品课(3倍)
            case "101010501":// 首页-正福易考通(3倍)
            case "102010201":// 采购人-高校院所(3倍)
            case "103010201":// 实务-亚利说采购(3倍)
            case "107010302":// 汽车-图例展示
            case "108010302":// IT-图例展示
            case "109010302":// 电梯-图例展示
            case "110010302":// 空调-图例展示
            case "111010302":// 家具-图例展示
                maxCount = maxCount * 3;
                break;
        }
        int haveCount = channelDataMapper.loadChannelDataCount(param.getPlateId());// 当前频道已推荐数量(包含非锁定状态)
        int lockCount = channelDataMapper.loadChannelDataLockCount(param.getPlateId());// 当前频道锁定状态的推荐数量

        // 锁定推荐数量不能超过配置的最大数量
        if (lockCount >= maxCount) return ResultUtil.error(ResponseEnum.CHANNEL_RECOMMEND_LIST_MAX_ERROR);
        if (haveCount < maxCount) {
            // 当前频道推荐还有空闲位置--直接修改当前数据为推荐状态
            channelDataMapper.updateChannelDataRecommendStatus(param.getId(), 1);
        } else {
            // 推荐位已满，替换掉已推荐中未锁定的数据(未锁定状态最早推荐的一条数据)
            int id = channelDataMapper.loadRecommendOldData(param.getPlateId()).getId();
            // 将旧数据的推荐状态去除
            channelDataMapper.updateChannelDataRecommendStatus(id, 0);
            // 设置新的推荐状态
            channelDataMapper.updateChannelDataRecommendStatus(param.getId(), 1);
        }
        int titleType = channelConfigMapper.loadTitleTypeConfig(param.getPlateId());
        ArticleTitle articleTitle = articleTitleMapper.loadArticleTitle(param.getArticleId(), titleType);
        if (null == articleTitle) {
            // 新增标题
            articleTitle = new ArticleTitle();
            articleTitle.setArticleId(param.getArticleId());
            articleTitle.setArticleTitleType(titleType);
            articleTitle.setArticleTitle(param.getArticleTitle());
            articleTitle.setArticleImageId(param.getArticleImageId());
            articleTitle.setArticleImage(param.getArticleImage());
            articleTitle.setArticleImageAlt(param.getArticleImageAlt());
            articleTitle.setArticleTitleStatus(1);
            articleTitleMapper.saveArticleTitleData(articleTitle);

            // 保存图片使用记录
            ChannelDataInfo channelDataInfo = new ChannelDataInfo();
            channelDataInfo.setPlateId(param.getPlateId());
            channelDataInfo.setArticleId(param.getArticleId());
            channelDataInfo.setFileId(param.getArticleImageId());
            channelDataInfo.setFileType(1);
            channelDataInfoMapper.saveChannelDataInfoData(channelDataInfo);
        } else {
            // 修改标题
            articleTitle.setArticleTitle(param.getArticleTitle());
            articleTitle.setArticleImageId(param.getArticleImageId());
            articleTitle.setArticleImage(param.getArticleImage());
            articleTitle.setArticleImageAlt(param.getArticleImageAlt());
            articleTitleMapper.updateArticleTitleData(articleTitle);

            // 保存图片使用记录
            ChannelDataInfo channelDataInfo = new ChannelDataInfo();
            channelDataInfo.setPlateId(param.getPlateId());
            channelDataInfo.setArticleId(param.getArticleId());
            channelDataInfo.setFileId(param.getArticleImageId());
            channelDataInfo.setFileType(1);
            channelDataInfoMapper.saveChannelDataInfoData(channelDataInfo);
        }

        // 推荐位新增完成之后进行静态页资源更新
        htmlHomePageBuilderComponent.buildHomePageHtml(param.getPlateId());
        // 同步更新处理
        if (param.getPlateId().equals("104010301")){
            // 当更新监管法律法规的时候--同步更新分站国家法规数据信息
            htmlHomePageBuilderComponent.buildHomePageHtml("201010104");// 河北--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("301010104");// 河南--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("401010104");// 山东--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("601010104");// 辽宁--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("901010104");// 辽宁--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1001010104");// 浙江--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1201010104");// 福建--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1401010104");// 湖北--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1701010104");// 海南--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1901010104");// 贵州--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2001010104");// 云南--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2101010104");// 陕西--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2401010104");// 北京--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2501010104");// 天津--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2601010104");// 重庆--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3801010104");// 深圳--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3501010104");// 青岛--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3401010104");// 大连--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3201010104");// 西藏--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3101010104");// 新疆--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3001010104");// 宁夏--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2901010104");// 广西--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2801010104");// 内蒙古--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2201010104");// 甘肃--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3301010104");// 新疆生产建设兵团--国家法规
        }
        return ResultUtil.success(ResponseEnum.SUCCESS);
    }

    @Override
    public Result loadChannelRecommendDataList(ChannelDataListParam param) {
        List<ChannelDataList> list = channelDataMapper.loadChannelRecommendDataList(param.getPlateId());
        int titleType = channelConfigMapper.loadTitleTypeConfig(param.getPlateId());
        list.forEach(item -> {
            // 获取改文章对应类型标题
            ArticleTitle articleTitle = articleTitleMapper.loadArticleTitle(item.getArticleId(), titleType);// 大列表标题类型固定为-1
            // 设置文章的标题类型数据
            item.setArticleTitle(articleTitle.getArticleTitle());
            item.setArticleTitleImage(articleTitle.getArticleImage());
            item.setArticleTitleImageAlt(articleTitle.getArticleImageAlt());
            item.setArticleTitleImageId(articleTitle.getArticleImageId());
        });
        return ResultUtil.success(list);
    }

    @Override
    public Result saveChannelRecommendData(ChannelDataParam param) {
        Article article = articleMapper.loadArticle(param.getArticleId());
        if (null == article) return Result.error(ResponseEnum.CHANNEL_RECOMMEND_LIST_MAX_ERROR);
        // 获取当前添加的内容是否存在于当前频道内
        ChannelData channelData = channelDataMapper.loadChannelIsHaveData(param.getPlateId(), param.getArticleId());
        // 当前添加的内容太已存在于当前频道并且已经被推荐
        if (null != channelData && channelData.getRecommendStatus() == 1)
            return Result.error(ResponseEnum.CHANNEL_RECOMMEND_LIST_EXITS_ERROR);
        // 获取当前频道配置的标题类型
        int titleType = channelConfigMapper.loadTitleTypeConfig(param.getPlateId());
        // 判定当前频道所配置的最大内容量
        int maxCount = channelMapper.loadChannelConfigCount(param.getPlateId());// 当前频道配置的最大数量
        // 倍数控制处理
        switch (param.getPlateId()) {
            case "101010301":// 首页-图表数据(2倍)
            case "104010201":// 监管-新规解读(2倍)
            case "105010201":// 研究院-政采大数据(2倍)
            case "107010201":// 汽车-图表数据
            case "108010201":// IT-图表数据
            case "109010201":// 电梯-图表数据
            case "110010201":// 空调-图表数据
            case "111010201":// 家具-图表数据
                maxCount = maxCount * 2;
                break;
            case "101010201":// 首页-易采通精品课(3倍)
            case "101010501":// 首页-正福易考通(3倍)
            case "102010201":// 采购人-高校院所(3倍)
            case "103010201":// 实务-亚利说采购(3倍)
            case "107010302":// 汽车-图例展示
            case "108010302":// IT-图例展示
            case "109010302":// 电梯-图例展示
            case "110010302":// 空调-图例展示
            case "111010302":// 家具-图例展示
                maxCount = maxCount * 3;
                break;
        }
        int haveCount = channelDataMapper.loadChannelDataCount(param.getPlateId());// 当前频道已推荐数量(包含非锁定状态)
        int lockCount = channelDataMapper.loadChannelDataLockCount(param.getPlateId());// 当前频道锁定状态的推荐数量
        // 锁定推荐数量不能超过配置的最大数量
        if (lockCount >= maxCount) return ResultUtil.error(ResponseEnum.CHANNEL_RECOMMEND_LIST_MAX_ERROR);
        if (null == channelData) {
            // 当前内容不在频道内--先添加到大列表中--再添加到推荐位中
            channelData = new ChannelData();
            channelData.setTitleType(titleType);
            channelData.setArticleId(param.getArticleId());
            channelData.setPlateId(param.getPlateId());
            channelData.setDataType(article.getArticleType());
            channelData.setShowIndex(0);
            channelData.setRecommendStatus(0);
            channelData.setStickStatus(0);
            channelData.setLockStatus(0);
            channelDataMapper.saveChannelData(channelData);
        }
        if (haveCount < maxCount) {
            // 当前频道推荐还有空闲位置--直接修改当前数据为推荐状态
            channelDataMapper.updateChannelDataRecommendStatus(channelData.getId(), 1);
        } else {
            // 推荐位已满，替换掉已推荐中未锁定的数据(未锁定状态最早推荐的一条数据)
            int id = channelDataMapper.loadRecommendOldData(param.getPlateId()).getId();
            // 将旧数据的推荐状态去除
            channelDataMapper.updateChannelDataRecommendStatus(id, 0);
            // 设置新的推荐状态
            channelDataMapper.updateChannelDataRecommendStatus(channelData.getId(), 1);
        }
        ArticleTitle articleTitle = articleTitleMapper.loadArticleTitle(param.getArticleId(), titleType);
        if (null == articleTitle) {
            // 新增标题
            articleTitle = new ArticleTitle();
            articleTitle.setArticleId(param.getArticleId());
            articleTitle.setArticleTitleType(titleType);
            articleTitle.setArticleTitle(param.getArticleTitle());
            articleTitle.setArticleImageId(param.getArticleImageId());
            articleTitle.setArticleImage(param.getArticleImage());
            articleTitle.setArticleImageAlt(param.getArticleImageAlt());
            articleTitle.setArticleTitleStatus(1);
            articleTitleMapper.saveArticleTitleData(articleTitle);

            // 保存图片使用记录
            ChannelDataInfo channelDataInfo = new ChannelDataInfo();
            channelDataInfo.setPlateId(param.getPlateId());
            channelDataInfo.setArticleId(param.getArticleId());
            channelDataInfo.setFileId(param.getArticleImageId());
            channelDataInfo.setFileType(1);
            channelDataInfoMapper.saveChannelDataInfoData(channelDataInfo);
        } else {
            // 修改标题
            articleTitle.setArticleTitle(param.getArticleTitle());
            articleTitle.setArticleImageId(param.getArticleImageId());
            articleTitle.setArticleImage(param.getArticleImage());
            articleTitle.setArticleImageAlt(param.getArticleImageAlt());
            articleTitleMapper.updateArticleTitleData(articleTitle);

            // 保存图片使用记录
            ChannelDataInfo channelDataInfo = new ChannelDataInfo();
            channelDataInfo.setPlateId(param.getPlateId());
            channelDataInfo.setArticleId(param.getArticleId());
            channelDataInfo.setFileId(param.getArticleImageId());
            channelDataInfo.setFileType(1);
            channelDataInfoMapper.saveChannelDataInfoData(channelDataInfo);
        }
        // 如果是第一次发布到频道，则生成文章的频道静态页
        int isPublic = articleHtmlMapper.loadArticleHtmlCount(param.getArticleId());
        if (isPublic == 1) {
            // 文章第一次发布--进行频道文章静态资源更新
            String url = htmlDetailBuilderComponent.buildArticleHtml(param.getArticleId(), param.getPlateId());
            redisObjectComponent.del(param.getArticleId());
            if (!url.equals("")) {
                ArticleHtml articleHtml = new ArticleHtml();
                articleHtml.setArticleId(param.getArticleId());
                articleHtml.setFolderUrl(url);
                articleHtmlMapper.saveArticleHtmlData(articleHtml);
            }
        }
        // 更新header中集采年会、监管峰会链接
        if (param.getPlateId().equals("100010401") || param.getPlateId().equals("100010402")) {
            htmlBuilderComponent.buildHeaderHtml("101");// 更新首页频道头
            htmlBuilderComponent.buildHeaderHtml("102");// 更新采购人频道头
            htmlBuilderComponent.buildHeaderHtml("103");// 更新采购实务频道头
            htmlBuilderComponent.buildHeaderHtml("104");// 更新监管频道头
            htmlBuilderComponent.buildHeaderHtml("105");// 更新研究院频道头
            htmlBuilderComponent.buildHeaderHtml("106");// 更新军队频道头
            htmlBuilderComponent.buildHeaderHtml("107");// 更新汽车频道头
            htmlBuilderComponent.buildHeaderHtml("108");// 更新IT频道头
            htmlBuilderComponent.buildHeaderHtml("109");// 更新电梯频道头
            htmlBuilderComponent.buildHeaderHtml("110");// 更新空调电器频道头
            htmlBuilderComponent.buildHeaderHtml("111");// 更新家具频道头
        }
        // 推荐位新增完成之后进行静态页资源更新
        htmlHomePageBuilderComponent.buildHomePageHtml(param.getPlateId());
        // 同步更新处理
        if (param.getPlateId().equals("104010301")){
            // 当更新监管法律法规的时候--同步更新分站国家法规数据信息
            htmlHomePageBuilderComponent.buildHomePageHtml("201010104");// 河北--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("301010104");// 河南--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("401010104");// 山东--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("601010104");// 辽宁--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("901010104");// 辽宁--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1001010104");// 浙江--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1201010104");// 福建--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1401010104");// 湖北--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1701010104");// 海南--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1901010104");// 贵州--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2001010104");// 云南--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2101010104");// 陕西--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2401010104");// 北京--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2501010104");// 天津--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2601010104");// 重庆--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3801010104");// 深圳--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3501010104");// 青岛--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3401010104");// 大连--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3201010104");// 西藏--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3101010104");// 新疆--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3001010104");// 宁夏--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2901010104");// 广西--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2801010104");// 内蒙古--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2201010104");// 甘肃--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3301010104");// 新疆生产建设兵团--国家法规
        }
        return ResultUtil.success(ResponseEnum.SUCCESS);
    }

    @Override
    public Result updateChannelRecommendData(ChannelDataParam param) {
        int titleType = channelConfigMapper.loadTitleTypeConfig(param.getPlateId());
        // 修改对应的标题数据
        ArticleTitle articleTitle = new ArticleTitle();
        articleTitle.setArticleId(param.getArticleId());
        articleTitle.setArticleTitleType(titleType);
        articleTitle.setArticleTitle(param.getArticleTitle());
        articleTitle.setArticleImage(param.getArticleImage());
        articleTitle.setArticleImageAlt(param.getArticleImageAlt());
        articleTitleMapper.updateArticleTitleDataBySelective(articleTitle);
        // 保存图片使用记录
        ChannelDataInfo channelDataInfo = new ChannelDataInfo();
        channelDataInfo.setPlateId(param.getPlateId());
        channelDataInfo.setArticleId(param.getArticleId());
        channelDataInfo.setFileId(param.getArticleImageId());
        channelDataInfo.setFileType(1);
        channelDataInfoMapper.saveChannelDataInfoData(channelDataInfo);
        // 推荐位新增完成之后进行静态页资源更新
        htmlHomePageBuilderComponent.buildHomePageHtml(param.getPlateId());
        // 更新header中集采年会、监管峰会链接
        if (param.getPlateId().equals("100010401") || param.getPlateId().equals("100010402")) {
            htmlBuilderComponent.buildHeaderHtml("101");// 更新首页频道头
            htmlBuilderComponent.buildHeaderHtml("102");// 更新采购人频道头
            htmlBuilderComponent.buildHeaderHtml("103");// 更新采购实务频道头
            htmlBuilderComponent.buildHeaderHtml("104");// 更新监管频道头
            htmlBuilderComponent.buildHeaderHtml("105");// 更新研究院频道头
            htmlBuilderComponent.buildHeaderHtml("106");// 更新军队频道头
            htmlBuilderComponent.buildHeaderHtml("107");// 更新汽车频道头
            htmlBuilderComponent.buildHeaderHtml("108");// 更新IT频道头
            htmlBuilderComponent.buildHeaderHtml("109");// 更新电梯频道头
            htmlBuilderComponent.buildHeaderHtml("110");// 更新空调电器频道头
            htmlBuilderComponent.buildHeaderHtml("111");// 更新家具频道头
        }
        // 同步更新处理
        if (param.getPlateId().equals("104010301")){
            // 当更新监管法律法规的时候--同步更新分站国家法规数据信息
            htmlHomePageBuilderComponent.buildHomePageHtml("201010104");// 河北--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("301010104");// 河南--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("401010104");// 山东--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("601010104");// 辽宁--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("901010104");// 辽宁--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1001010104");// 浙江--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1201010104");// 福建--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1401010104");// 湖北--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1701010104");// 海南--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1901010104");// 贵州--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2001010104");// 云南--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2101010104");// 陕西--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2401010104");// 北京--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2501010104");// 天津--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2601010104");// 重庆--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3801010104");// 深圳--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3501010104");// 青岛--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3401010104");// 大连--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3201010104");// 西藏--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3101010104");// 新疆--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3001010104");// 宁夏--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2901010104");// 广西--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2801010104");// 内蒙古--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2201010104");// 甘肃--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3301010104");// 新疆生产建设兵团--国家法规
        }
        // 修改任何一项文章内容，发送ESMQ消息
        amqpTemplate.convertAndSend(Constant.CMS_ARTICLE_QUEUE, Collections.singletonList(new EsArticleMqVO(param.getArticleId(), Constant.UPDATE_ARTICLE)));
        return ResultUtil.success(ResponseEnum.SUCCESS);
    }

    @Override
    public Result loadChannelTitleConfig(ChannelDataParam param) {
        int titleType = channelConfigMapper.loadTitleTypeConfig(param.getPlateId());
        ArticleDictionary titleDictionary = articleDictionaryMapper.loadDictionaryByTitleType(titleType);
        return Result.success(titleDictionary);
    }

    @Override
    public Result loadChannelRecommendDataInfo(ChannelDataParam param) {
        ChannelRecommendDataInfo channelData = new ChannelRecommendDataInfo();
        Article article = articleMapper.loadArticle(param.getArticleId());
        if (param.getPlateId().equals("107020201") && article.getArticleType() != 7){
            // 好车汇无法添加产品以外的类型
            return ResultUtil.error(ResponseEnum.CHANNEL_RECOMMEND_LIST_TYPE_ERROR);
        }
        if (param.getPlateId().equals("100010101") && article.getArticleType() != 8) {
            // 精品好书无法添加图书类型以外的文章
            return ResultUtil.error(ResponseEnum.CHANNEL_RECOMMEND_LIST_TYPE_ERROR);
        }
        if (param.getPlateId().equals("100010201") && article.getArticleType() != 6) {
            // 亚利聊政采无法添加亚利聊政采类型以外的文章
            return ResultUtil.error(ResponseEnum.CHANNEL_RECOMMEND_LIST_TYPE_ERROR);
        }
        int titleType = channelConfigMapper.loadTitleTypeConfig(param.getPlateId());
        ArticleTitle articleTitle = articleTitleMapper.loadArticleTitle(param.getArticleId(), titleType);
        if (null == articleTitle) {
            // 没有对应的标题类型列表--获取默认的列表标题
            articleTitle = articleTitleMapper.loadArticleTitle(param.getArticleId(), -1);
        }
        ArticleDictionary articleDictionary = articleDictionaryService.loadDictionaryByTitleType(titleType);
        channelData.setArticleId(articleTitle.getArticleId());
        channelData.setArticleTitleType(articleTitle.getArticleTitleType());
        channelData.setArticleTitle(articleTitle.getArticleTitle());
        channelData.setArticleImageId(articleTitle.getArticleImageId());
        channelData.setArticleImage(articleTitle.getArticleImage());
        channelData.setArticleImageAlt(articleTitle.getArticleImageAlt());
        channelData.setTitleSize(articleDictionary.getTitleSize());
        channelData.setTitleImageWidth(articleDictionary.getTitleImageWidth());
        channelData.setTitleImageHeight(articleDictionary.getTitleImageHeight());
        return Result.success(channelData);
    }

    @Override
    public Result updateChannelDataLockStatus(ChannelDataParam param) {
        int isRecommend = channelDataMapper.loadChannelDataRecommendStatus(param.getId());
        if (isRecommend == 0) return Result.error(ResponseEnum.CHANNEL_LOCK_ERROR);
        channelDataMapper.updateChannelDataLockStatus(param.getId(), param.getLockStatus());
        return Result.success(ResponseEnum.SUCCESS);
    }

    @Override
    public Result updateChannelDataShowIndex(List<ChannelDataParam> list) {
        String plateId = "";
        for (ChannelDataParam info : list) {
            channelDataMapper.updateChannelDataShowIndex(info.getId(), info.getShowIndex());
            plateId = info.getPlateId();
        }
        htmlHomePageBuilderComponent.buildHomePageHtml(plateId);
        // 同步更新处理
        if (plateId.equals("104010301")){
            // 当更新监管法律法规的时候--同步更新分站国家法规数据信息
            htmlHomePageBuilderComponent.buildHomePageHtml("201010104");// 河北--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("301010104");// 河南--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("401010104");// 山东--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("601010104");// 辽宁--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("901010104");// 辽宁--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1001010104");// 浙江--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1201010104");// 福建--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1401010104");// 湖北--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1701010104");// 海南--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1901010104");// 贵州--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2001010104");// 云南--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2101010104");// 陕西--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2401010104");// 北京--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2501010104");// 天津--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2601010104");// 重庆--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3801010104");// 深圳--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3501010104");// 青岛--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3401010104");// 大连--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3201010104");// 西藏--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3101010104");// 新疆--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3001010104");// 宁夏--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2901010104");// 广西--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2801010104");// 内蒙古--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2201010104");// 甘肃--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3301010104");// 新疆生产建设兵团--国家法规
        }
        return Result.success(ResponseEnum.SUCCESS);
    }

    @Override
    public Result loadChannelOtherConfig(ChannelParam param) {
        return Result.success(channelMapper.loadChannelOtherConfig(param.getChannelId()));
    }

    @Override
    public Result updateChannelOtherConfig(ChannelOtherConfigData param) {
        channelMapper.updateChannelOtherConfig(param);
        if (
                param.getChannelId().equals("101") || param.getChannelId().equals("102") || param.getChannelId().equals("103") ||
                param.getChannelId().equals("104") || param.getChannelId().equals("105") || param.getChannelId().equals("106") ||
                param.getChannelId().equals("107") || param.getChannelId().equals("108") || param.getChannelId().equals("109") ||
                param.getChannelId().equals("110") || param.getChannelId().equals("111") || param.getChannelId().equals("100010601") ||

                param.getChannelId().equals("201") || param.getChannelId().equals("301") || param.getChannelId().equals("401") ||
                param.getChannelId().equals("501") || param.getChannelId().equals("601") || param.getChannelId().equals("701") ||
                param.getChannelId().equals("801") || param.getChannelId().equals("901") || param.getChannelId().equals("1001") ||
                param.getChannelId().equals("1101") || param.getChannelId().equals("1201") || param.getChannelId().equals("1301") ||
                param.getChannelId().equals("1401") || param.getChannelId().equals("1501") || param.getChannelId().equals("1601") ||
                param.getChannelId().equals("1701") || param.getChannelId().equals("1801") || param.getChannelId().equals("1901") ||
                param.getChannelId().equals("2001") || param.getChannelId().equals("2101") || param.getChannelId().equals("2201") ||
                param.getChannelId().equals("2301") || param.getChannelId().equals("2401") || param.getChannelId().equals("2501") ||
                param.getChannelId().equals("2601") || param.getChannelId().equals("2701") || param.getChannelId().equals("2801") ||
                param.getChannelId().equals("2901") || param.getChannelId().equals("3001") || param.getChannelId().equals("3101") ||
                param.getChannelId().equals("3201") || param.getChannelId().equals("3301") || param.getChannelId().equals("3401") ||
                param.getChannelId().equals("3501") || param.getChannelId().equals("3601") || param.getChannelId().equals("3701") ||
                param.getChannelId().equals("3801")) {
            // 当更新的是各个频道首页信息、获取全局手机二维码信息的时候，触发更新页头html静态页事件
            htmlBuilderComponent.buildHeaderHtml(param.getChannelId());
            // 更新频道首页
            htmlHomePageBuilderComponent.buildHomePageConfigHtml(param.getChannelId());
            // 更新频道官微
            htmlHomePageBuilderComponent.buildHomePageHibuick(param.getChannelId());
            // 更新频道左侧LOGO
            if (param.getChannelId().equals("102") || param.getChannelId().equals("103") || param.getChannelId().equals("104") ||
                    param.getChannelId().equals("105") || param.getChannelId().equals("106")) {
                htmlHomePageBuilderComponent.buildHomePageLeftLogo(param.getChannelId());
            } else if (param.getChannelId().equals("101")){
                // 如果更新的是网站首页--同步更新全局的标签列表页
                htmlListPageBuilderComponent.buildListPageConfigHtml("100");
            }
            htmlListPageBuilderComponent.buildListPageConfigHtml(param.getChannelId());
        }
        htmlHomePageBuilderComponent.buildHomePageHtml(param.getChannelId());
        // 同步更新处理
        if (param.getChannelId().equals("104010301")){
            // 当更新监管法律法规的时候--同步更新分站国家法规数据信息
            htmlHomePageBuilderComponent.buildHomePageHtml("201010104");// 河北--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("301010104");// 河南--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("401010104");// 山东--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("601010104");// 辽宁--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("901010104");// 辽宁--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1001010104");// 浙江--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1201010104");// 福建--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1401010104");// 湖北--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1701010104");// 海南--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1901010104");// 贵州--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2001010104");// 云南--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2101010104");// 陕西--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2401010104");// 北京--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2501010104");// 天津--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2601010104");// 重庆--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3801010104");// 深圳--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3501010104");// 青岛--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3401010104");// 大连--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3201010104");// 西藏--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3101010104");// 新疆--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3001010104");// 宁夏--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2901010104");// 广西--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2801010104");// 内蒙古--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2201010104");// 甘肃--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3301010104");// 新疆生产建设兵团--国家法规
        }
        return Result.success(ResponseEnum.SUCCESS);
    }

    @Override
    public Result loadCollectionSubstationChannelList(String userId) {
        List<DictionaryList> list = collectionService.loadCollectionListDictionary();
        //由于要和首页跳转权限一致,返回数据前先去权限表做校验
        List<UserPrivilege> privilege = userPrivilegeMapper.findPurchasePagePrivilege(userId);
        List<Integer> codes = privilege.stream().map(bean -> {
            String privilegeCode = bean.getPrivilegeCode();
            String last2Code = privilegeCode.substring(privilegeCode.length() - 2);
            if (last2Code.startsWith("0"))
                return Integer.parseInt(privilegeCode.substring(privilegeCode.length() - 1));
            return Integer.parseInt(last2Code);
        }).collect(Collectors.toList());

        //将采购网与权限进行筛选过滤
        List<DictionaryList> dictionaryLists = list.stream().filter(bean -> codes.contains(Integer.parseInt(bean.getValue()))).collect(Collectors.toList());

        List<Map<String, Object>> maps = disponsePinyin(dictionaryLists);
        return Result.success(maps);
    }

    @Override
    public Result savePublishPrivileges(StaffPublishParam param) {
        List<ChannelLevel> channelLevel = channelMapper.findAllChannelLevel();
        if (CollectionUtils.isEmpty(channelLevel)) {
            return Result.error(ResponseEnum.NOT_FIND_DATA);
        }
        List<String> manageValues = channelLevel.stream().map(ChannelLevel::getChannelValue).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(param.getPrivilegeCodes())) {
            for (String code : param.getPrivilegeCodes()) {
                if (!manageValues.contains(code)) {
                    log.info("未知权限参数={}", code);
                    return Result.error(ResponseEnum.UNKNOWN_PLATE_ID);
                }
            }
        }
        //查询用户
        UserBaseInfo staff = userBaseInfoMapper.findStaffByStaffId(param.getStaffId());
        if (staff == null) {
            return Result.error(ResponseEnum.NO_FIND_STAFF);
        }
        //先清除用户之前数据
        if (!CollectionUtils.isEmpty(param.getPrivilegeCodes())) {
            int deleteNum = userPrivilegeMapper.deleteStaffPrivilegesByUserId(staff.getUserid(), Constant.PUBLISH_TYPE);
            List<UserPrivilege> userPrivileges = new ArrayList<>();
            param.getPrivilegeCodes().forEach(privilegeCode -> {
                if (privilegeCode.length() > 2) {
                    UserPrivilege privilege = new UserPrivilege();
                    privilege.setCreateTime(new Date());
                    privilege.setPrivilegeCode(String.valueOf(privilegeCode));
                    privilege.setPrivilegeType(Constant.PUBLISH_TYPE);
                    //privilege.setPrivilegeLevel();
                    privilege.setUserid(staff.getUserid());
                    userPrivileges.add(privilege);
                }
            });
            int insertNum = userPrivilegeMapper.saveStaffPrivilegesByUserId(userPrivileges);
            //返回成功保存的条数
            return Result.success(insertNum);
        } else {
            int deleteNum = userPrivilegeMapper.deleteStaffPrivilegesByUserId(staff.getUserid(), Constant.PUBLISH_TYPE);
            log.info("deleteNum={}",deleteNum);
        }
        //删除用户现有权限 并返回0
        return Result.success(0);
    }

    @Override
    public Result findPublishPrivileges(StaffPublishParam param) {
        if (StringUtils.isEmpty(param.getStaffId())) {
            return Result.error(ResponseEnum.NO_STAFF_ID);
        }
        UserBaseInfo staff = userBaseInfoMapper.findStaffByStaffId(param.getStaffId());
        List<ChannelLevel> channelLevels = channelMapper.findAllChannelLevel();
        if (CollectionUtils.isEmpty(channelLevels)) {
            return Result.error(ResponseEnum.NOT_FIND_DATA);
        }
        StaffPrivilegesVO privilegesVO = new StaffPrivilegesVO();
        Map<String, List<ChannelLevel>> articleDicMap = channelLevels.stream().collect(Collectors.groupingBy(ChannelLevel::getSuperId));
        Map<String, ChannelLevel> dictionaryMap = channelLevels.stream().collect(Collectors.toMap(ChannelLevel::getChannelValue, bean -> bean));
        List<FunctionPrivilegeVO> functionPrivilegeVOS = new ArrayList<>();
        List<UserPrivilege> userPrivileges = userPrivilegeMapper.findUserOperatePrivilegeByUserId(staff.getUserid(), Constant.PUBLISH_TYPE);
        List<String> privilegeCodes = userPrivileges.stream().map(UserPrivilege::getPrivilegeCode).collect(Collectors.toList());
        setPublishPrivileges(articleDicMap, functionPrivilegeVOS, "1", dictionaryMap, privilegeCodes);
        privilegesVO.setKeyValueVOS(functionPrivilegeVOS);
        privilegesVO.setPrivilegeCode(privilegeCodes);
        return Result.success(privilegesVO);
    }

    @Override
    public Result findStaffPublishPrivileges(String userId) {
        List<PrivilegeVO> privilegesOrg = new ArrayList<>();
        List<UserPrivilege> userPrivileges = userPrivilegeMapper.findUserOperatePrivilegeByUserId(userId, Constant.PUBLISH_TYPE);
        //查询出第四级权限
        List<String> privilege4Codes = userPrivileges.stream().map(UserPrivilege::getPrivilegeCode)
                .filter(privilegeCode -> privilegeCode.length() == 9)
                .filter(privilegeCode -> !Arrays.asList("100010601", "100010701", "100010702", "100010801", "100010901",
                        "100011001", "106020301", "101010501", "101020301", "101020501", "102020301", "103020301", "104020301",
                        "105020301", "106020301"
                ).contains(privilegeCode)).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(privilege4Codes)) {
            return Result.success(Collections.emptyList());
        }
        List<ChannelLevel> channel4Values = channelMapper.findChannelDictionaryByChannelValues(privilege4Codes);
        channel4Values.forEach(bean -> {
            privilegesOrg.add(new PrivilegeVO(bean.getChannelValue(), bean.getChannelName(), 4, bean.getSuperId()));
        });
        //查询第三级权限
        List<String> privilege3Codes = channel4Values.stream().map(ChannelLevel::getSuperId).distinct().collect(Collectors.toList());
        List<ChannelLevel> channel3Values = channelMapper.findChannelDictionaryByChannelValues(privilege3Codes);
        channel3Values.forEach(bean -> {
            privilegesOrg.add(new PrivilegeVO(bean.getChannelValue(), bean.getChannelName(), 3, bean.getSuperId()));
        });

        //查询第二级权限
        List<String> privilege2Codes = channel3Values.stream().map(ChannelLevel::getSuperId).distinct().collect(Collectors.toList());
        List<ChannelLevel> channel2Values = channelMapper.findChannelDictionaryByChannelValues(privilege2Codes);
        channel2Values.forEach(bean -> {
            privilegesOrg.add(new PrivilegeVO(bean.getChannelValue(), bean.getChannelName(), 2, bean.getSuperId()));
        });

        //查询第一级权限
        List<String> privilege1Codes = channel2Values.stream().map(ChannelLevel::getSuperId).distinct().collect(Collectors.toList());
        List<ChannelLevel> channel1Values = channelMapper.findChannelDictionaryByChannelValues(privilege1Codes);
        channel1Values.forEach(bean -> {
            privilegesOrg.add(new PrivilegeVO(bean.getChannelValue(), bean.getChannelName(), 1, bean.getSuperId()));
        });

        Map<String, List<PrivilegeVO>> functionBlockMap = privilegesOrg.stream().collect(Collectors.groupingBy(PrivilegeVO::getParentPrivilegeCode));
        List<PrivilegeVO> privilegeVOList = new ArrayList<>();


        setChildPrivilegeVo(functionBlockMap, privilegeVOList, "1", 1);

        delete4PrivilegeVO(privilegeVOList, 1);

        List<PrivilegeVO> collect = privilegeVOList.stream().peek(privilege -> {
            privilege.setPrivilegeCode("100".equals(privilege.getPrivilegeCode()) ? "120" : privilege.getPrivilegeCode());
        }).sorted(Comparator.comparing(PrivilegeVO::getPrivilegeCode)).collect(Collectors.toList());
        return Result.success(collect);
    }

    @Override
    public Result findStaffHomePagePrivilege(HomePageParam param, String userId) {
        List<UserPrivilege> userPrivileges = userPrivilegeMapper.findUserOperatePrivilegeByUserId(userId, Constant.FUNCTION_TYPE);
        //遍历链接枚举类型  与 员工所有权限比较  包含权限则返回true
        for (PrivilegeEnum privilegeEnum : PrivilegeEnum.values()) {
            if (param.getPrivilegeName().equals(privilegeEnum.getPrivilegeName())) {
                String enumCode = privilegeEnum.getCode();
                if (!CollectionUtils.isEmpty(userPrivileges)) {
                    return Result.success(userPrivileges.stream().map(UserPrivilege::getPrivilegeCode).collect(Collectors.toList()).contains(enumCode));
                } else {
                    return Result.success(false);
                }
            }
        }
        return Result.success(false);
    }

    @Override
    public Result<StaffPrivilegesVO> loadChannels(String userId) {
        StaffPrivilegesVO privilegesVO = new StaffPrivilegesVO();
        List<ArticleDictionary> channelStatusVOS = articleDictionaryMapper.findAllChannelSectionList();
        Map<Integer, List<ArticleDictionary>> articleDicMap = channelStatusVOS.stream().collect(Collectors.groupingBy(ArticleDictionary::getSuperId));
        Map<Integer, ArticleDictionary> dictionaryMap = channelStatusVOS.stream().collect(Collectors.toMap(ArticleDictionary::getValue, bean -> bean));
        List<FunctionPrivilegeVO> functionPrivilegeVOS = new ArrayList<>();
        setChildBusinessVo(articleDicMap, functionPrivilegeVOS, 0, dictionaryMap);
        userPrivilegeMapper.findUserOperatePrivilegeByUserId(userId, Constant.MANAGE_TYPE);
        privilegesVO.setKeyValueVOS(functionPrivilegeVOS);
        List<UserPrivilege> userPrivileges = userPrivilegeMapper.findUserOperatePrivilegeByUserId(userId, Constant.MANAGE_TYPE);
        List<String> privilegeCodes = userPrivileges.stream().map(UserPrivilege::getPrivilegeCode).collect(Collectors.toList());
        privilegesVO.setPrivilegeCode(privilegeCodes);
        return Result.success(privilegesVO);
    }

    @Override
    public Result<Integer> saveStaffManagePrivilegesByUserId(List<Integer> privilegeCodes, String userId) {
        if (CollectionUtils.isEmpty(privilegeCodes)) {
            return Result.error(ResponseEnum.PARAM_ERROR);
        }
        List<ArticleDictionary> channelDictionaryList = articleDictionaryMapper.findAllChannelDictionaryList();
        List<Integer> values = channelDictionaryList.stream().map(ArticleDictionary::getValue).collect(Collectors.toList());
        List<ArticleDictionary> articleDictionaries = articleDictionaryMapper.findAllSectionDictionaryByValues(values);
        List<Integer> manageValues = articleDictionaries.stream().map(ArticleDictionary::getValue).collect(Collectors.toList());
        for (Integer code : privilegeCodes) {
            if (!manageValues.contains(code)) {
                log.info("未知权限参数={}", code);
                return Result.error(ResponseEnum.UNKNOWN_CHANNEL_ID);
            }
        }
        //先清除用户之前数据
        int deleteNum = userPrivilegeMapper.deleteStaffPrivilegesByUserId(userId, Constant.MANAGE_TYPE);
        Map<Integer, Integer> allChannel = channelDictionaryList.stream().collect(Collectors.toMap(ArticleDictionary::getValue, ArticleDictionary::getLevel));
        Map<Integer, Integer> allSection = articleDictionaries.stream().collect(Collectors.toMap(ArticleDictionary::getValue, ArticleDictionary::getLevel));
        allChannel.putAll(allSection);
        List<UserPrivilege> userPrivileges = new ArrayList<>();
        privilegeCodes.forEach(privilegeCode -> {
            UserPrivilege privilege = new UserPrivilege();
            privilege.setCreateTime(new Date());
            privilege.setPrivilegeCode(String.valueOf(privilegeCode));
            privilege.setPrivilegeType(Constant.MANAGE_TYPE);
            privilege.setPrivilegeLevel(allChannel.get(privilegeCode));
            privilege.setUserid(userId);
            userPrivileges.add(privilege);
        });
        int insertNum = userPrivilegeMapper.saveStaffPrivilegesByUserId(userPrivileges);
        return Result.success(insertNum);
    }

    @Override
    public Result findSectionStatus(SectionStatusParam param) {
        PageHelper.startPage(param.getPage(), param.getSize());
        PageInfo<ChannelStatusVO> channelStatusVOS = new PageInfo<>(articleDictionaryMapper.findChannelSectionStatus(param.getKeyword()));
        return Result.success(channelStatusVOS);
    }

    @Override
    public Result updateSectionStatus(SectionStatusParam param) {
        //验证section(栏目值是否正确)
        if (param.getSection() == null || param.getCommentStatus() == null) {
            return Result.error(ResponseEnum.PRIVILEGE_ERROR);
        }
        List<ArticleDictionary> sectionDictionaryList = articleDictionaryMapper.findSectionDictionaryByLevel(2);
        List<Integer> values = sectionDictionaryList.stream().map(ArticleDictionary::getValue).collect(Collectors.toList());
        if (!values.contains(param.getSection())) {
            return Result.error(ResponseEnum.NO_FIND_SECTION_VALUE);
        }
        ArticleDictionary articleDictionary = articleDictionaryMapper.findDictionaryByTypeAndValueAndLevel(3, param.getSection(), 2);
        if (articleDictionary != null) {
            ChannelStatusVO dictionary = new ChannelStatusVO();
            dictionary.setId(articleDictionary.getId());
            dictionary.setCommentStatus(param.getCommentStatus());
            int updateNum = articleDictionaryMapper.updateByPrimaryKeySelective(dictionary);
            if (updateNum == 0) {
                return Result.error(ResponseEnum.REQUEST_ABORTED);
            } else {
                return Result.success(updateNum);
            }
        }
        return Result.error(ResponseEnum.NO_FIND_SECTION_VALUE);
    }

    @Override
    public Result findStaffManagePrivilegesByUserId(String userId) {
        StaffManageSectionVO manageSectionVO = new StaffManageSectionVO();
        List<FunctionPrivilegeVO> functionPrivilegeVOS = articleDictionaryService.findStaffManagePrivilegesByUserId(userId);
        //        //2、用户权限
        List<UserPrivilege> userPrivileges = userPrivilegeMapper.findUserOperatePrivilegeByUserId(userId, Constant.MANAGE_TYPE);
        List<String> privilegeCodes = userPrivileges.stream().map(UserPrivilege::getPrivilegeCode).collect(Collectors.toList());
        manageSectionVO.setSectionCode(privilegeCodes);
        manageSectionVO.setKeyValueVOS(functionPrivilegeVOS);
        return Result.success(manageSectionVO);
    }

    @Override
    public Result loadChannelHotSearchData(FrontChannelDataPram param) {
        int dateSql = 24;
        switch (param.getQueryType()) {
            case 2:
                dateSql = 168;
                break;
            case 3:
                dateSql = 720;
                break;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        Date endDate = calendar.getTime();
        calendar.add(Calendar.HOUR, 0 - dateSql);//24小时、168小时、720小时前
        Date startDate = calendar.getTime();

        String redisKey = "";
        List<ChannelHotSearchVo> list;
        //String plateKey = param.getPlateId() == 101 ? "000": param.getPlateId().toString();
        String strPlateId = param.getPlateId();
        if (param.getQueryType() == 2) {
            // 周
            redisKey = "channelHotSearchDataWeek:" + strPlateId;
        } else if (param.getQueryType() == 3) {
            // 月
            redisKey = "channelHotSearchDataMonth:" + strPlateId;
        } else {
            // 天
            redisKey = "channelHotSearchDataDay:" + strPlateId;
        }
        list = (List<ChannelHotSearchVo>) redisObjectComponent.get(redisKey);// 先从缓存中获取数据
        if (null == list || list.size() == 0) {
            PageHelper.startPage(param.getPageNum(), param.getPageSize());
            if (!param.getPlateId().equals("101")) {// 网站首页查询全部数据，因此不设置频道条件SQL
                //list = channelDataMapper.loadChannelHotSearchData(strPlateId, dateSql);
                log.info("获取频道点击量排行榜的时间段startDate={}, endDate={}, plateId={}", startDate, endDate, param.getPlateId());
                list = articleStatisticsDayMapper.loadHotArticleByChannel(startDate, endDate, strPlateId);
            } else {
                // 取全站点击量, plateId=101
                //list = channelDataMapper.loadChannelHotSearchDataALL(dateSql);
                log.info("获取所有频道点击量排行榜的时间段startDate={}, endDate={}, plateId={}", startDate, endDate, param.getPlateId());
                list = articleStatisticsDayMapper.loadHotArticleAllChannel(startDate, endDate);
            }
            redisObjectComponent.set(redisKey, list, 86400);//日周月排行榜都设置24个小时redis缓存过期时间
        }
        // 遍历处理数据
        list.forEach(item -> {
            ArticleTitle articleTitle = articleTitleMapper.loadArticleTitle(item.getArticleId(), -1);
            if (null != articleTitle) {
                item.setArticleTitle(articleTitle.getArticleTitle());
                item.setArticleTitleImage(articleTitle.getArticleImage());
                item.setArticleTitleImageAlt(articleTitle.getArticleImageAlt());
            }
        });
        PageInfo<ChannelHotSearchVo> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }

    @Override
    public List<ChannelDataList> loadChannelRecommendDataList(String plateId) {
        return channelDataMapper.loadChannelRecommendDataList(plateId);
    }

    @Override
    public HeaderConfig loadChannelHeaderConfig(String plateId) {
        return channelMapper.loadChannelHeaderConfig(plateId);
    }

    @Override
    public Channel getChannelByPlateId(String plateId) {
        return channelMapper.getChannelByPlateId(plateId);
    }

    @Override
    public Channel loadChannelByPlate(String plateId) {
        return channelMapper.loadChannelByPlate(plateId);
    }

    @Override
    public Channel loadChannelByArticle(String articleId) {
        return channelMapper.loadChannelByArticle(articleId);
    }

    @Override
    public List<Channel> listChannelByChannelType(Integer channelType) {
        return channelMapper.listChannelByChannelType(channelType);
    }

    @Override
    public List<Channel> listChannelByChannelValues(List<String> channelValues) {
        return channelMapper.listChannelByChannelValues(channelValues);
    }

    @Override
    public List<Channel> selectChannelBySuperIdAndChannelType(String superId, Integer channelType) {
        return channelMapper.selectChannelBySuperIdAndChannelType(superId, channelType);
    }

    @Override
    public boolean deleteChannelHotSearchDataFromRedis(FrontChannelDataPram param) {
        String redisKey = "";
        String strPlateId = param.getPlateId();
        if (param.getQueryType() == 2) {
            // 周
            redisKey = "channelHotSearchDataWeek:" + strPlateId;
        } else if (param.getQueryType() == 3) {
            // 月
            redisKey = "channelHotSearchDataMonth:" + strPlateId;
        } else {
            // 天
            redisKey = "channelHotSearchDataDay:" + strPlateId;
        }
        return redisObjectComponent.del(redisKey);
    }

    /**
     * 频道自动推荐(不进行文章标题处理)
     * @param articleId 文章ID
     * @param channelId 所属频道
     * @return
     */
    @Override
    public boolean updateChannelDataRecommend(String articleId, String channelId) {
        // 判定当前频道所配置的最大内容量
        int maxCount = channelMapper.loadChannelConfigCount(channelId);// 当前频道配置的最大数量
        // 倍数控制处理
        switch (channelId) {
            case "101010301":// 首页-图表数据(2倍)
            case "104010201":// 监管-新规解读(2倍)
            case "105010201":// 研究院-政采大数据(2倍)
            case "107010201":// 汽车-图表数据
            case "108010201":// IT-图表数据
            case "109010201":// 电梯-图表数据
            case "110010201":// 空调-图表数据
            case "111010201":// 家具-图表数据
                maxCount = maxCount * 2;
                break;
            case "101010201":// 首页-易采通精品课(3倍)
            case "101010501":// 首页-正福易考通(3倍)
            case "102010201":// 采购人-高校院所(3倍)
            case "103010201":// 实务-亚利说采购(3倍)
            case "107010302":// 汽车-图例展示
            case "108010302":// IT-图例展示
            case "109010302":// 电梯-图例展示
            case "110010302":// 空调-图例展示
            case "111010302":// 家具-图例展示
                maxCount = maxCount * 3;
                break;
        }
        int haveCount = channelDataMapper.loadChannelDataCount(channelId);// 当前频道已推荐数量(包含非锁定状态)
        int lockCount = channelDataMapper.loadChannelDataLockCount(channelId);// 当前频道锁定状态的推荐数量
        // 锁定推荐数量不能超过配置的最大数量
        if (lockCount >= maxCount) return false;
        if (haveCount < maxCount) {// 当前频道推荐还有空闲位置--直接修改当前数据为推荐状态
            channelDataMapper.updateChannelDataRecommendStatusByArticleAndChannel(articleId,channelId,1);
        } else {
            // 推荐位已满，替换掉已推荐中未锁定的数据(未锁定状态最早推荐的一条数据)
            int id = channelDataMapper.loadRecommendOldData(channelId).getId();
            // 将旧数据的推荐状态去除
            channelDataMapper.updateChannelDataRecommendStatus(id, 0);
            // 设置新的推荐状态
            channelDataMapper.updateChannelDataRecommendStatusByArticleAndChannel(articleId,channelId,1);
        }
        // 推荐位新增完成之后进行静态页资源更新
        htmlHomePageBuilderComponent.buildHomePageHtml(channelId);
        // 同步更新处理
        if (channelId.equals("104010301")){
            // 当更新监管法律法规的时候--同步更新分站国家法规数据信息
            htmlHomePageBuilderComponent.buildHomePageHtml("201010104");// 河北--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("301010104");// 河南--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("401010104");// 山东--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("601010104");// 辽宁--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("901010104");// 辽宁--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1001010104");// 浙江--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1201010104");// 福建--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1401010104");// 湖北--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1701010104");// 海南--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("1901010104");// 贵州--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2001010104");// 云南--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2101010104");// 陕西--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2401010104");// 北京--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2501010104");// 天津--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2601010104");// 重庆--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3801010104");// 深圳--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3501010104");// 青岛--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3401010104");// 大连--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3201010104");// 西藏--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3101010104");// 新疆--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3001010104");// 宁夏--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2901010104");// 广西--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2801010104");// 内蒙古--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("2201010104");// 甘肃--国家法规
            htmlHomePageBuilderComponent.buildHomePageHtml("3301010104");// 新疆生产建设兵团--国家法规
        }
        return true;
    }

    private void setPublishPrivileges(Map<String, List<ChannelLevel>> privilegesMap, List<FunctionPrivilegeVO> keyValueVOS, String start,
                                      Map<String, ChannelLevel> functionBlockMap, List<String> privilegeCodes) {
        List<ChannelLevel> child = privilegesMap.get(start);
        if (!CollectionUtils.isEmpty(child)) {
            child.forEach(vo -> {
                FunctionPrivilegeVO keyValueVO = new FunctionPrivilegeVO();
                keyValueVO.setLabel(vo.getChannelName());
                keyValueVO.setValue(vo.getChannelValue());
                if (privilegeCodes.contains(String.valueOf(vo.getChannelValue()))) {
                    keyValueVO.setSelected(true);
                } else {
                    keyValueVO.setSelected(false);
                }
                keyValueVOS.add(keyValueVO);
                setPublishPrivileges(privilegesMap, keyValueVO.getChildren(), vo.getChannelValue(), functionBlockMap, privilegeCodes);
            });
        }
    }

    private void setChildBusinessVo(Map<Integer, List<ArticleDictionary>> PrivilegesMap, List<FunctionPrivilegeVO> keyValueVOS,
                                    Integer start, Map<Integer, ArticleDictionary> functionBlockMap) {
        List<ArticleDictionary> child = PrivilegesMap.get(start);
        if (!CollectionUtils.isEmpty(child)) {
            child.forEach(vo -> {
                FunctionPrivilegeVO keyValueVO = new FunctionPrivilegeVO();
                keyValueVO.setLabel(vo.getName());
                keyValueVO.setValue(vo.getValue());
                keyValueVOS.add(keyValueVO);
                setChildBusinessVo(PrivilegesMap, keyValueVO.getChildren(), vo.getValue(), functionBlockMap);
            });
        }
    }

    private void getStaffAllCodes(List<PrivilegeVO> privilegeVOS, List<String> allPrivilegeCodes) {
        if (!CollectionUtils.isEmpty(privilegeVOS)) {
            privilegeVOS.forEach(privilegeVO -> {
                allPrivilegeCodes.add(privilegeVO.getPrivilegeCode());
                getStaffAllCodes(privilegeVO.getChildPrivilegeVos(), allPrivilegeCodes);
            });
        }
    }

    private void delete4PrivilegeVO(List<PrivilegeVO> privilegeVOList, Integer level) {
        if (!CollectionUtils.isEmpty(privilegeVOList)) {
            privilegeVOList.forEach(bean -> {
                if (level == 2) {
                    List<PrivilegeVO> list = new ArrayList<>();
                    if (!CollectionUtils.isEmpty(bean.getChildPrivilegeVos())) {
                        List<PrivilegeVO> childPrivilegeVos = bean.getChildPrivilegeVos();
                        childPrivilegeVos.forEach(bean2 -> {
                            if (!CollectionUtils.isEmpty(bean2.getChildPrivilegeVos())) {
                                List<PrivilegeVO> childPrivilegeVos1 = bean2.getChildPrivilegeVos();
                                list.addAll(childPrivilegeVos1);
                            }
                        });
                    }
                    bean.setChildPrivilegeVos(list);
                }
                delete4PrivilegeVO(bean.getChildPrivilegeVos(), level + 1);
            });
        }
    }

    private void setChildPrivilegeVo(Map<String, List<PrivilegeVO>> privilegesMap, List<PrivilegeVO> privilegeVOList,
                                     String parentPrivilegeCode, Integer level) {
        List<PrivilegeVO> privilegeVOS = privilegesMap.get(parentPrivilegeCode);
        if (!CollectionUtils.isEmpty(privilegeVOS)) {
            privilegeVOS.forEach(vo -> {
                PrivilegeVO privilegeVO = new PrivilegeVO();
                privilegeVO.setPrivilegeName(vo.getPrivilegeName());
                privilegeVO.setPrivilegeCode(vo.getPrivilegeCode());
                privilegeVO.setPrivilegeLevel(vo.getPrivilegeLevel());
                privilegeVO.setPrivilegeIcon(vo.getPrivilegeIcon());
                privilegeVOList.add(privilegeVO);
                setChildPrivilegeVo(privilegesMap, privilegeVO.getChildPrivilegeVos(), vo.getPrivilegeCode(), (1 + level));
            });
        }
    }

    private static List<Map<String, Object>> disponsePinyin(List<DictionaryList> list) {
        List<String> name = new ArrayList<>();
        List<Map<String, Object>> maps = new ArrayList<>();// 字母列表
        if (null != list) {
            // 拼装所有的字母列表
            list.forEach(item -> {
                String initials = PinyinUtil.getPinyinInitials(item.getLabel());
                if (!initials.matches("[a-zA-Z]")) initials = "#";
                if (!name.contains(initials)) {// 当前字母不存在与列表中
                    Map<String, Object> map = new HashMap<>();
                    map.put("title", initials);
                    List<Map<String, Object>> b = new ArrayList<>();
                    Map<String, Object> map2 = new HashMap<>();
                    map2.put("name", item.getLabel());
                    map2.put("id", item.getValue());
                    b.add(map2);
                    map.put("label", b);
                    maps.add(map);
                    name.add(initials);
                } else {// 当前字母存在列表中
                    int index = name.indexOf(initials);
                    List<Map<String, Object>> a = (List<Map<String, Object>>) maps.get(index).get("label");// 地区列表
                    List<Map<String, Object>> b = new ArrayList<>();// 需要填充地区属性
                    Map<String, Object> map = new HashMap<>();
                    map.put("name", item.getLabel());
                    map.put("id", item.getValue());
                    b.add(map);
                    if (null == a) {
                        maps.get(index).put("label", b);
                    } else {// 如果已有列表则执行填充该数据
                        ((List<Map<String, Object>>) maps.get(index).get("label")).add(map);
                    }
                }
            });
        }
        maps.sort(Comparator.comparing(o -> o.get("title").toString()));
        return maps;
    }
}
